package com.rjet.view_demo1;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import razerdp.basepopup.BasePopupWindow;

public class Demo2Popup extends BasePopupWindow {

    public static final String TAG = "czy";

    //回调编辑的数量
    interface NumEditListener{

        void finishEdit(String num, Integer tag);
        void minus(Integer tag);
        void plus(Integer tag);
    }

    private NumEditListener numEditListener;

    public void setNumEditListener(NumEditListener numEditListener) {
        this.numEditListener = numEditListener;
    }

    private final Object tag;

    public Demo2Popup(Context context, Object tag) {
        super(context);

        this.tag = tag;

        View v = createPopupById(R.layout.pop_view2);
        setContentView(v);

        LinearLayout pop2_parent = v.findViewById(R.id.pop2_parent);
        CornerMaker.makeCorner(pop2_parent, 10, Color.WHITE);

        EditText numed = v.findViewById(R.id.numed);
        TextView sure = v.findViewById(R.id.sure);
        CornerMaker.makeCorner(sure, 5, ContextCompat.getColor(context, R.color.colorAccent));
        sure.setTag((Integer)this.tag+100);

        TextView minus = v.findViewById(R.id.minus);
        minus.setTag((Integer)this.tag+200);

        TextView plus = v.findViewById(R.id.plus);
        plus.setTag((Integer)this.tag+300);

        //减少
        minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: 减少: " + minus.getTag());

                Integer ednum = Integer.valueOf(numed.getText().toString());

                if (ednum <= 1){
                    Toast.makeText(context, "不能再少了", Toast.LENGTH_SHORT).show();
                    return;
                }

                numed.setText(String.valueOf(ednum-1));
                numEditListener.minus((Integer)minus.getTag()-401);
            }
        });

        //增加
        plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: 增加: " + plus.getTag());
                numed.setText(String.valueOf(Integer.valueOf(numed.getText().toString())+1));
                numEditListener.plus((Integer)plus.getTag()-501);
            }
        });

        //确定
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d(TAG, "onClick: " + numed.getText().toString() + " tag:" + sure.getTag());
                numEditListener.finishEdit(numed.getText().toString(), (Integer)sure.getTag()-301);
            }
        });

    }


}
